<?php
session_start();
set_include_path('./../../nearestCaches/library' . PATH_SEPARATOR . get_include_path());
$PEAR_PATH = 'PEAR.php';
include ('Request/Request.php');
include "functions.php";
$lang = getLang();
$versionInt = getClientIntVersion();

global $viewstate;
global $postback;
global $cookie;

if ($_GET['show'] == 'search') {
	$waypoint = $_POST["waypoint"];
	$url = "http://www.geocaching.com/seek/cache_details.aspx?wp=".urlencode($waypoint)."&Submit6=Go";
    	$req = & new HTTP_Request($url);
    	$req->setMethod(HTTP_REQUEST_METHOD_GET);
    	$req->addHeader('User-Agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6');
    	$req->addCookie('ASP.NET_SessionId', $_SESSION['cookie']);
    	$req->sendRequest();
    	$response = $req->getResponseBody();
    	
    	if (strpos($response, 'Cache is Unpublished')) {
    	echo '<?xml version="1.0" encoding="UTF-8"?>
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
            <html>
              <head>
                <title>Warning</title>
              </head>
              <body class="alert">
            <p class="text">'.$lang['unpublished'].'.</p>        <locify:timeout value="6000" />';
    	} else {
    		$response = parseBetween('<span id="ctl00_ContentBody_CacheOwner">', '</span>');
    		$position = 0;
    		$viewstate = parseBetween('&wid=', '&ds=');
    		header ('Location: '.SERVICE_URL.'cache.php?guid='.$viewstate);
    	}




    } else {
	echo '<?xml version="1.0" encoding="utf-8" ?>
		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
		<html xmlns="http://www.w3.org/1999/xhtml" xmlns:locify="http://client.locify.com/locify_ns/1.0" xml:lang="en" lang="en">
		<head>
    		<title>'.$lang['waypoint_search'].'</title>
  		</head>
  		<body class="form">
    		<form action="'.SERVICE_URL.'waypoint.php?show=search" method="post">
		<fieldset>
			<b>'.$lang['gc_code'].':</b> <br />
			<label>&nbsp;<input type="text" name="waypoint" value="GC" /></label><br />
         		<input type="submit" value="'.$lang['search'].'" /><br />
		</fieldset>
    		</form>
  		</body>
		</html>';

}
?>
